import {
  ACCESS_TOKEN_KEY,
  MENUS_KEY,
  USER_INFO_KEY,
  USER_NAME_KEY,
} from "@/enums/cacheEnum";
import { localCache } from "@/utils/storage";
import { defineStore } from "pinia";
import router from "@/router/index";
import { userManager } from "@/api/ajaxUrl.config";
import { getAction, postAction } from "@/api/manage";
export const userStore = defineStore("useBattleStore", {
  state: () => {
    return {
      token: "",
      userName: "",
      userInfo: {},
      routerList: [],
    };
  },
  actions: {
    setMenus() {
      return new Promise((resolve, reject) => {
        try {
          const menus = localCache.getCache(MENUS_KEY);
          this.routerList = menus;
          resolve(true);
        } catch {
          reject(false);
        }
      });
    },
    // 获取后台返回的路由
    getRouter({ userName, password }: { userName: string; password: string }) {
      return new Promise((resolve, reject) => {
        getAction(userManager.permission, {
          userName: userName,
          password: password,
        })
          .then((res: any) => {
            if (res.success) {
              const object = res.data;
              // 将菜单信息存入 pinia
              this.routerList = object.permissions;
              // 将菜单信息存入缓存
              localCache.setCache(MENUS_KEY, object.permissions);
              resolve(res);
            } else {
              reject(res.message);
            }
          })
          .catch((error) => {
            reject(error);
          });
      });
    },
    login({ userName, password }: { userName: string; password: string }) {
      return new Promise((resolve, reject) => {
        //模拟登陆，获取用户信息， 权限路由列表
        postAction(userManager.login, {
          userName: userName,
          password: password,
        })
          .then((res: any) => {
            if (res.success) {
              const object = res.data;
              // 将用户信息存入 pinia
              this.userInfo = object.userInfo;
              this.userName = object.userInfo.userName;
              // 将用户信息存入缓存
              localCache.setCache(ACCESS_TOKEN_KEY, object.token);
              localCache.setCache(USER_NAME_KEY, object.userInfo.userName);
              localCache.setCache(USER_INFO_KEY, object.userInfo);
              resolve(res);
            } else {
              reject(res.message);
            }
          })
          .catch((error) => {
            reject(error);
          });
      });
    },

    // 注销用户
    logout() {
      return new Promise((resolve) => {
        //拷贝一下
        const delRouterList = JSON.parse(JSON.stringify(this.routerList));
        //删除添加的路由，如果路由是多层的 递归下。。
        delRouterList.forEach((route: any) => {
          router.removeRoute(route.name);
        });
        //删除路由
        this.clearUser();
        this.clearToken();
        this.clearRouterList();
        localCache.clearCache();
        resolve("注销 success， 清空路由，用户信息");
      });
    },
    // 清空路由数据
    clearRouterList() {
      this.routerList = [];
    },
    clearToken() {
      this.token = "";
    },
    clearUser() {
      this.userName = "";
      this.userInfo = {};
    },
  },
  share: {
    enable: true,
    initialize: true,
  },
});
